module.exports = (options) => {
  const jwt = require("jsonwebtoken"); // 引入token模块
  const AdminUser = require("../models/AdminUser"); // 引入用户模块
  const assert = require("http-assert"); // 引入http-assets模块
  // 登录校验中间件
  return async (req, res, next) => {
    const token = String(req.headers.authorization || "")
      .split(" ")
      .pop(); // 获得请求携带的token值,将请求后的值进行格式化处理，用split方法进行分割最后弹出最后一个元素
    assert(token, 401, "请先登录");
    const { id } = jwt.verify(token, req.app.get("secret"));
    assert(id, 401, "无效的jwt token");
    req.user = await AdminUser.findById(id);
    assert(req.user, 401, "请先登录");
    await next();
  };
};
